package org.malred.hotelsystem.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.malred.hotelsystem.model.entity.OrderEntity;
import org.malred.hotelsystem.model.entity.UserEntity;

import java.util.ArrayList;

public class UserDao {
    private static final String TAG = "UserDao";
    private static final String TB_NAME = "tb_user";

    /**
     * 添加
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean add(SQLiteDatabase db, UserEntity entity) {
        ContentValues values = new ContentValues();
        values.put("u_id", entity.getId());
        values.put("u_name", entity.getName());
        values.put("u_pass", entity.getPass());
        values.put("u_role", entity.getRole());
        long i = db.insert(TB_NAME, null, values);
        return i > 0;
    }

    /**
     * 根据id删除
     *
     * @param db
     * @param id
     * @return
     */
    public static boolean del(SQLiteDatabase db, String id) {
        long i = db.delete(TB_NAME, "u_id=?", new String[]{id});
        return i > 0;
    }

    /**
     * 更新
     *
     * @param db
     * @param entity
     * @return
     */
    public static boolean upt(SQLiteDatabase db, UserEntity entity) {
        ContentValues values = new ContentValues();
        values.put("u_name", entity.getName());
        values.put("u_pass", entity.getPass());
        values.put("u_role", entity.getRole());
        int i = db.update(TB_NAME, values, "u_id=?", new String[]{entity.getId()});
        return i > 0;
    }

    /**
     * 查询所有
     *
     * @param db
     * @return
     */
    public static ArrayList<UserEntity> getAll(SQLiteDatabase db) {
        ArrayList<UserEntity> items = new ArrayList();
        Cursor c = db.query(TB_NAME, null, null,
                null, null, null, null);
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String name = c.getString(c.getColumnIndex("u_name"));
            @SuppressLint("Range") String pass = c.getString(c.getColumnIndex("u_pass"));
            @SuppressLint("Range") String role = c.getString(c.getColumnIndex("u_role"));
            UserEntity userEntity = new UserEntity();
            userEntity.setId(id);
            userEntity.setName(name);
            userEntity.setPass(pass);
            userEntity.setRole(role);
            items.add(userEntity);
        }
        c.close();
        return items;
    }

    /**
     * 根据id查询
     *
     * @param db
     * @param idVal
     * @return
     */
    public static UserEntity getById(SQLiteDatabase db, String idVal) {
        Cursor c = db.query(
                TB_NAME, null,
                "u_id=?", new String[]{idVal},
                null, null,
                null
        );
        UserEntity userEntity = new UserEntity();
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String name = c.getString(c.getColumnIndex("u_name"));
            @SuppressLint("Range") String pass = c.getString(c.getColumnIndex("u_pass"));
            @SuppressLint("Range") String role = c.getString(c.getColumnIndex("u_role"));
            userEntity.setId(id);
            userEntity.setName(name);
            userEntity.setPass(pass);
            userEntity.setRole(role);
        }
        c.close();
        return userEntity;
    }

    public static UserEntity login(SQLiteDatabase db,String name,String pass) {
        Cursor c = db.query(
                TB_NAME, null,
                "u_name=? and u_pass=?", new String[]{name,pass},
                null, null,
                null
        );
        UserEntity userEntity = new UserEntity();
        while (c.moveToNext()) {
            @SuppressLint("Range") String id = c.getString(c.getColumnIndex("u_id"));
            @SuppressLint("Range") String uname = c.getString(c.getColumnIndex("u_name"));
            @SuppressLint("Range") String upass = c.getString(c.getColumnIndex("u_pass"));
            @SuppressLint("Range") String role = c.getString(c.getColumnIndex("u_role"));
            userEntity.setId(id);
            userEntity.setName(uname);
            userEntity.setPass(upass);
            userEntity.setRole(role);
        }
        c.close();
        return userEntity;
    }
}
